package Presentacion.academico;

import clases.academico.Academico;
import javax.swing.table.DefaultTableModel;

public final class frmConsultaAcademico extends javax.swing.JInternalFrame {

    private static int pagina;
    private static String sql;
    public static Academico objAcademico;
    public static dlgAcademico dlgacademico;

    /**
     * Creates new form frmConsultaConcepto
     */
    public frmConsultaAcademico() {
        initComponents();
        setResizable(false);
        objAcademico = new Academico();
        pagina = 1;
        sql = objAcademico.SqlGeneralAcademico();
        generartabla();
        inicializar();
    }

    public void mostrarAcademico() {
        dlgacademico = new dlgAcademico(null, true);
        dlgacademico.setVisible(true);
        dlgacademico.visiblidad(true);
    }

    public void llenarDialogo(int fila) {     
        dlgacademico=new dlgAcademico(null,true);
        objAcademico.llenardatosAcademicos(dlgAcademico.modelo, 
                dlgAcademico.tblAcademico, tblAcademico.getValueAt(fila, 1).toString());
        dlgAcademico.txtcedula.setText(tblAcademico.getValueAt(fila, 1).toString());
        dlgAcademico.txtalumno.setText(tblAcademico.getValueAt(fila, 2).toString());
        dlgacademico.visiblidad(false);
        dlgacademico.btnbuscar.setVisible(false);
        dlgacademico.setVisible(true);
    }

    public static void generartabla() {
        objAcademico.ConsultaAcademico(new DefaultTableModel(), tblAcademico,
                sql, lbltotalpaginas, lblpagina, pagina);
    }

    private void inicializar() {
        txtalumno.setText("");
        txtalumno.setVisible(false);
        txtcedula.setText("");
        txtcedula.setVisible(false);
        cboacademico.setVisible(false);    
    }
 
    public void EliminarAcademico(int fila) {
        if (objAcademico.eliminarAcademico(tblAcademico.getValueAt(fila, 3).toString(),
                tblAcademico.getValueAt(fila, 1).toString()) == true) {
            generartabla();
        }
    }


    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblAcademico = new javax.swing.JTable();
        jToolBar1 = new javax.swing.JToolBar();
        btnprimero = new javax.swing.JButton();
        btnatras = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        jLabel4 = new javax.swing.JLabel();
        lblpagina = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        lbltotalpaginas = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        btnsiguiente = new javax.swing.JButton();
        btnultimo = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        cbocriterio = new javax.swing.JComboBox();
        txtalumno = new javax.swing.JTextField();
        txtcedula = new javax.swing.JTextField();
        cboacademico = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        tblAcademico.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblAcademico.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblAcademicoMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblAcademico);

        jToolBar1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        btnprimero.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/primer.png"))); // NOI18N
        btnprimero.setFocusable(false);
        btnprimero.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnprimero.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnprimero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnprimeroActionPerformed(evt);
            }
        });
        jToolBar1.add(btnprimero);

        btnatras.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/atras.png"))); // NOI18N
        btnatras.setToolTipText("");
        btnatras.setFocusable(false);
        btnatras.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnatras.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnatras.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnatrasActionPerformed(evt);
            }
        });
        jToolBar1.add(btnatras);
        jToolBar1.add(jSeparator1);

        jLabel4.setText("Pagina ");
        jToolBar1.add(jLabel4);

        lblpagina.setText("jLabel2");
        jToolBar1.add(lblpagina);

        jLabel5.setText(" de ");
        jToolBar1.add(jLabel5);

        lbltotalpaginas.setText("jLabel1");
        jToolBar1.add(lbltotalpaginas);
        jToolBar1.add(jSeparator2);

        btnsiguiente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/siguiente.png"))); // NOI18N
        btnsiguiente.setFocusable(false);
        btnsiguiente.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnsiguiente.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnsiguiente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnsiguienteActionPerformed(evt);
            }
        });
        jToolBar1.add(btnsiguiente);

        btnultimo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/ultimos.png"))); // NOI18N
        btnultimo.setFocusable(false);
        btnultimo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnultimo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnultimo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnultimoActionPerformed(evt);
            }
        });
        jToolBar1.add(btnultimo);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 511, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "BUSQUEDA", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setText("CRITERIO");
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 30, -1, -1));

        cbocriterio.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "TODO", "CEDULA", "ALUMNO", "DATO ACADÉMICO" }));
        cbocriterio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbocriterioActionPerformed(evt);
            }
        });
        jPanel1.add(cbocriterio, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 50, 140, -1));

        txtalumno.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtalumnoKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtalumnoKeyTyped(evt);
            }
        });
        jPanel1.add(txtalumno, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 50, 260, -1));

        txtcedula.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtcedulaKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtcedulaKeyTyped(evt);
            }
        });
        jPanel1.add(txtcedula, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 50, 130, -1));

        cboacademico.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboacademicoActionPerformed(evt);
            }
        });
        jPanel1.add(cboacademico, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 50, 260, -1));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 535, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnsiguienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnsiguienteActionPerformed
       // objAcademico = new Academico();
        if (objAcademico.objPaginacion.getTotalpaginas() > pagina) {
            pagina++;
            generartabla();
        }
    }//GEN-LAST:event_btnsiguienteActionPerformed

    private void btnultimoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnultimoActionPerformed
        pagina = objAcademico.objPaginacion.getTotalpaginas();
        generartabla();
    }//GEN-LAST:event_btnultimoActionPerformed

    private void btnatrasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnatrasActionPerformed
        if (pagina > 1) {
            pagina--;
            generartabla();
        }
    }//GEN-LAST:event_btnatrasActionPerformed

    private void btnprimeroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnprimeroActionPerformed
        pagina = 1;
        generartabla();
    }//GEN-LAST:event_btnprimeroActionPerformed

    private void tblAcademicoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblAcademicoMouseClicked
        int click = evt.getClickCount();
        if (click == 2) {
            llenarDialogo(tblAcademico.getSelectedRow());
        }
    }//GEN-LAST:event_tblAcademicoMouseClicked

    private void cbocriterioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbocriterioActionPerformed
        inicializar();
        if (cbocriterio.getSelectedItem() == "TODO") {
            pagina = 1;
            sql = objAcademico.SqlGeneralAcademico();
            generartabla();
        } else if (cbocriterio.getSelectedItem() == "ALUMNO") {
            txtalumno.setVisible(true);
            txtalumno.requestFocus();
        } else if (cbocriterio.getSelectedItem() == "CEDULA") {
            txtcedula.setVisible(true);
            txtcedula.requestFocus();
        } else if (cbocriterio.getSelectedItem() == "DATO ACADÉMICO") {
            objAcademico.listarDatoAcademico(cboacademico);
            cboacademico.setVisible(true);
            cboacademico.requestFocus();
        } 
    }//GEN-LAST:event_cbocriterioActionPerformed

    private void txtalumnoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtalumnoKeyReleased
        global.Validaciones.convertirMayuscula(txtalumno);
        if ((global.Validaciones.comprobarNulo(txtalumno))) {
            pagina = 1;
            sql = objAcademico.SqlAlumnoAcademico(txtalumno.getText());
            generartabla();
        } 
    }//GEN-LAST:event_txtalumnoKeyReleased

    private void txtalumnoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtalumnoKeyTyped
        global.Validaciones.soloLetras(evt, txtalumno);
    }//GEN-LAST:event_txtalumnoKeyTyped

    private void txtcedulaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtcedulaKeyReleased
        if ((global.Validaciones.comprobarNulo(txtcedula))) {
            pagina = 1;
            sql = objAcademico.SqlCedulaAcademico(txtcedula.getText());
            generartabla();
        } 

    }//GEN-LAST:event_txtcedulaKeyReleased

    private void txtcedulaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtcedulaKeyTyped
        global.Validaciones.soloNumeros(evt, txtcedula, 10);
    }//GEN-LAST:event_txtcedulaKeyTyped

    private void cboacademicoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboacademicoActionPerformed
        if (cboacademico.getSelectedIndex() != 0) {
            pagina = 1;
            sql = objAcademico.SqlDatoAcademico(cboacademico.getSelectedItem().toString());
            generartabla();
        }
    }//GEN-LAST:event_cboacademicoActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frmConsultaAcademico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frmConsultaAcademico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frmConsultaAcademico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frmConsultaAcademico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new frmConsultaAcademico().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnatras;
    private javax.swing.JButton btnprimero;
    private javax.swing.JButton btnsiguiente;
    private javax.swing.JButton btnultimo;
    public static javax.swing.JComboBox cboacademico;
    private javax.swing.JComboBox cbocriterio;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JToolBar jToolBar1;
    private static javax.swing.JLabel lblpagina;
    private static javax.swing.JLabel lbltotalpaginas;
    public static javax.swing.JTable tblAcademico;
    private javax.swing.JTextField txtalumno;
    private javax.swing.JTextField txtcedula;
    // End of variables declaration//GEN-END:variables
}
